package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.exception.NumberGreaterMaxException;
import com.itheima.exception.NumberLessOrEqualZeroException;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.result.PageResult;
import com.itheima.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class StudentServiceimpl implements StudentService{


    /**
     * 注入mapper
     */
    private final StudentMapper studentMapper;

//    @Autowired
//    private StudentMapper studentMapper;


    /**
     * 分页查询
     */
    @Override
    public PageResult queryString(StudentQueryString stringQueryParam) {
        PageHelper.startPage(stringQueryParam.getPage(), stringQueryParam.getPageSize());
        Page<Student> page = studentMapper.selectPage (stringQueryParam);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除学生
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    /**
     * 添加学生
     */
    @Override
    public void add(Student student){
        studentMapper.insert(student);
    }

    /**
     * 根据id查询学生
     */
    @Override
    public Student getById(Integer id) {
        return studentMapper.selectById(id);
    }

    /**
     * 修改学生信息
     * @param student
     */
    @Override
    public void alter(Student student) {
         studentMapper.updateById(student);
    }

    /**
     * 违纪处理学生分数
     * @param id
     * @param score
     */
    @Override
    public void handle(Integer id, Integer score) {
        //判断违纪分数是否大于0
        if (score <=0){
            throw new NumberLessOrEqualZeroException(MessageConstant.VIOLATION_SCORE_NOT_LESS_OR_EQUAL_ZERO);
        }
        Student  student = studentMapper.selectById(id);
        //违纪次数加一
        student.setViolationCount(student.getViolationCount()+1);
        //违纪分数累加
        student.setViolationScore(student.getViolationScore()+score);

        //违纪分数不能大于100分
        if (student.getViolationScore()>100){
            throw new NumberGreaterMaxException(MessageConstant.VIOLATION_SCORE_NOT_GREATER_MAX);
        }
        studentMapper.updateById(student);
    }

}
